﻿using LightCAD.Core;
using LightCAD.Core.Elements;
using LightCAD.Drawing;
using LightCAD.Runtime;
using LightCAD.Three;
using QdArch.ThreeUtils;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QdArch
{
    public partial class WindowAction : ElementAction
    {
        public const double frameWidth = 50;
        public override BufferGeometry[] CreateGeometry3D(LcElement element, Matrix3d matrix)
        {
            var window = element as QdWindowRef;
            var thickness=window.Thickness;
            var glassThickness = 10;
            var bottom = window.Bottom;
            var height = window.Height;
            var width = window.Width;
            var start2 = window.StartPoint.ToThree();
            var start = new Vector3(start2, bottom);
            var end2 = window.EndPoint.ToThree();
            var end = new Vector3(end2, bottom);
            var lineVec = new Vector3().subVectors(end, start);
            var len = lineVec.length();
            var xAxis = lineVec.clone().normalize();
            var yAxis = VectorUtil.ZAxis.clone();
            var zAxis = new Vector3().crossVectors(xAxis, yAxis).normalize();
            var coodMat = new Matrix4();
            coodMat.makeBasis(xAxis, yAxis, zAxis);
            coodMat.setPosition(start);
            var glassShape = ShapeUtil.Rect(frameWidth, frameWidth, len- frameWidth, height- frameWidth);
            var frameShape = ShapeUtil.Rect(0, 0, len, height);
            frameShape.holes.Add(glassShape);
            var glassGeo= GeoUtil.GetStretchGeometryData(glassShape, coodMat, glassThickness / 2, -glassThickness / 2).GetBufferGeometry();
            glassGeo.computeVertexNormals();
            var frameGeo = GeoUtil.GetStretchGeometryData(frameShape, coodMat, thickness / 2, -thickness / 2).GetBufferGeometry();
            frameGeo.computeVertexNormals();
            return new BufferGeometry[] { frameGeo,glassGeo };
        }
    }
}
